package com.xiaoming;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.*;

/***
 * 主类
 */
public class LintcBootstrap {

    /***
     * 初始化
     */
    public void start(int port) throws Exception {
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Linus 的 Tomcat 开始启动,port：" + port);
        Map<String, HttpServlet> servletMap = new ServerResolver().loadServer();
        int corePoolSize = 10;
        int maximumPoolSize = 50;
        long keepAliveTime = 100L;
        TimeUnit unit = TimeUnit.MINUTES;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
                unit, workQueue, threadFactory, handler);
        while (true) {
            Socket socket = serverSocket.accept();
            RequestProcessor processor = new RequestProcessor(socket, servletMap);
            executor.execute(processor);
        }

    }



    /***
     * 启动函数
     * @param args
     */
    public static void main(String[] args) {
        LintcBootstrap lintcBootstrap = new LintcBootstrap();
        try {
            lintcBootstrap.start(9090);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
